package tsj.ai.alibaba.data.analysis.controller;

import com.alibaba.cloud.ai.graph.OverAllState;
import com.alibaba.cloud.ai.graph.RunnableConfig;
import com.alibaba.cloud.ai.graph.StateGraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static tsj.ai.alibaba.data.analysis.constant.Constant.*;

/**
 * 简单的聊天功能
 *
 * @author taoshujian
 * @version 1.0
 * @since 2025-10-28 11:00:01
 */
@RestController
@RequestMapping("/api/hello")
public class HelloController {

    private static final Logger log = LoggerFactory.getLogger(HelloController.class);
    @Autowired
    private StateGraph stateGraph;



    @GetMapping(value = "/query")
    public String query(@RequestParam(value = "query", defaultValue = "去年销量最高的商品", required = false) String query) throws Exception{
        RunnableConfig runnableConfig = RunnableConfig.builder().threadId("threadId").build();
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put(INPUT_KEY, query);
        objectMap.put(AGENT_ID, AGENTId);
        Optional<OverAllState> call = stateGraph.compile().call(objectMap, runnableConfig);
        OverAllState state = call.orElseThrow();

        log.info("QUERY_REWRITE_NODE_OUTPUT: {}", state.value(QUERY_REWRITE_NODE_OUTPUT));
        log.info("KEYWORD_EXTRACT_NODE_OUTPUT: {}", state.value(KEYWORD_EXTRACT_NODE_OUTPUT));
        log.info("SCHEMA_RECALL_NODE_OUTPUT: {}", state.value(SCHEMA_RECALL_NODE_OUTPUT));
        log.info("TABLE_RELATION_OUTPUT: {}", state.value(TABLE_RELATION_OUTPUT));
        log.info("PLANNER_NODE_OUTPUT: {}", state.value(PLANNER_NODE_OUTPUT));

        Optional<Object> result = state.value(PLANNER_NODE_OUTPUT);
        if (result.isPresent()) {
            return result.get().toString();
        } else {
            return "未找到结果";
        }
    }



}
